package com.bluesmart.mia;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sys.DemoApp;
import com.util.Constant;
import com.util.ImageLoaderHelper;
import com.util.MgqDataHandler;
import com.util.MgqRestClient;
import com.util.PreferencesUtils;
import com.util.ToastUtil;
import com.widget.CircleImageView;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class BabyProfileActivity extends Fragment implements OnClickListener{
	String milkId;
	String tempId;
	String bottleId;
	
	View mView = null;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,  
            Bundle savedInstanceState) {

		mView = inflater.inflate(R.layout.activity_baby_profile, container, false);  
		
		mView.findViewById(R.id.btn0).setOnClickListener(this);
		mView.findViewById(R.id.btn1).setOnClickListener(this);
		mView.findViewById(R.id.btn2).setOnClickListener(this);
		
		return mView;
	}
	
	private void sendHttpGetRequest() {
		MgqDataHandler loginHandler = new MgqDataHandler(getActivity(), false, false) {
			@Override
			public void onSuccess(String response) {
				JSONObject obj = JSON.parseObject(response);
				
				String tem = obj.getString("drinktemp");
				if(tem == null) tem = "";
				((TextView)mView.findViewById(R.id.btn1)).setText(tem);
				
				String milk = obj.getString("milkname").replace("\r\n", "");
				((TextView)mView.findViewById(R.id.btn0)).setText(milk);
				milkId = obj.getString("milkid");
				
				JSONArray array = obj.getJSONArray("bottlelist");
				if(array.size() > 0)
				{
					obj = array.getJSONObject(0);
					milk = obj.getString("bottlename").replace("\r\n", "");
					((TextView)mView.findViewById(R.id.btn2)).setText(milk);
					bottleId = obj.getString("bottleid");
				}
			}
		};
		
		JSONObject obj = ((DemoApp)getActivity().getApplicationContext()).getSeletedBaby();

		JSONObject params = new JSONObject();
		params.put("mia_un", PreferencesUtils.getString(getActivity(), "username"));
		params.put("pwd", PreferencesUtils.getString(getActivity(), "password"));
		params.put("babyid", obj.getString("babyid"));
		params.put("handType", "getpreferences");

		MgqRestClient.postBody(Constant.SERVER_URL + "/sw/babyInfoHandler.ashx", JSON.toJSONString(params), loginHandler);
	}
	
	@Override
	public void onResume()
	{
		super.onResume();
		sendHttpGetRequest();
	}
	
	@Override
	public void onClick(View arg0) {
		switch(arg0.getId())
		{
		case R.id.btn0:
		{
			Intent intent = new Intent(getActivity(),BabyProfileMilkActivity.class);
			intent.putExtra("id", bottleId);
			intent.putExtra("tmp", ((TextView)mView.findViewById(R.id.btn1)).getText().toString());
			startActivityForResult(intent, 10);
		}
			break;

		case R.id.btn1:
		{
			Intent intent = new Intent(getActivity(),BabyProfileTempActivity.class);
			intent.putExtra("bottle", bottleId);
			intent.putExtra("id", milkId);
			startActivityForResult(intent, 12);
		}
			break;
			
		case R.id.btn2:
		{
			Intent intent = new Intent(getActivity(),BabyProfileBottleActivity.class);
			intent.putExtra("id", milkId);
			intent.putExtra("tmp", ((TextView)mView.findViewById(R.id.btn1)).getText().toString());
			startActivityForResult(intent, 11);
		}
			break;
			
		case R.id.right:
			sendHttpRequest();
			break;
			
		case R.id.left:
			break;
		}
	}
	/*
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (data == null)
			return;

		if (resultCode == RESULT_OK) {
			if(requestCode == 10)
			{
				milkId = data.getStringExtra("typeId");
				((TextView)mView.findViewById(R.id.btn0)).setText(data.getStringExtra("value"));
				return;
			}
			else if(requestCode == 11)
			{
				bottleId = data.getStringExtra("typeId");
				((TextView)mView.findViewById(R.id.btn2)).setText(data.getStringExtra("value"));
				return;
			}
			else if(requestCode == 12)
			{
				tempId = String.valueOf(data.getStringExtra("value"));
				((TextView)mView.findViewById(R.id.btn1)).setText(tempId);
				return;
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	*/
	private void sendHttpRequest() {
		MgqDataHandler loginHandler = new MgqDataHandler(getActivity(), true, false) {
			@Override
			public void onSuccess(String response) {

				JSONObject ruser = JSON.parseObject(response);

				if (ruser.getString("resCode").equals("1000")) {
					ToastUtil.showToastWithAlertPic(R.string.success);
				}
				else if (ruser.getString("resCode").equals("2000")) {
					ToastUtil.showToastWithAlertPic(R.string.error_2000);
				}
				else if (ruser.getString("resCode").equals("3000")) {
					ToastUtil.showToastWithAlertPic(R.string.error_3000);
				}
				else 
				{
				}
			}
		};
		
		JSONObject obj = ((DemoApp)getActivity().getApplicationContext()).getSeletedBaby();

		JSONObject params = new JSONObject();
		params.put("mia_un", PreferencesUtils.getString(getActivity(), "username"));
		params.put("pwd", PreferencesUtils.getString(getActivity(), "password"));
		params.put("babyid", obj.getString("babyid"));
		params.put("handType", "addpreferences");
		params.put("bottleid", bottleId);
		params.put("milkid", milkId);
		
		MgqRestClient.postBody(Constant.SERVER_URL + "/sw/babyInfoHandler.ashx", JSON.toJSONString(params), loginHandler);
	}
}
